import $api from '@/api/apiList'
const { wallpaper } = $api
export const state = () => ({
    picData: [],
    cateList: [],
    total: 0
})
const setPicUrl = (source) => {
    return source.map((el, index) => {
        el.odd = Math.floor(index / 4) % 2 === 1
        el.even = (index + 1) % 2 === 0
        if (!el.realUrl) {
            el.realUrl = el.url.replace('w=1080', 'w=1200')
            el.url = el.url.replace('w=1080', 'w=290')
        }
        el.style = {
            'animation-delay': (Math.random() * 3 + 0.1).toFixed(1) + 's'
        }
        return el
    })
}
export const mutations = {
    setData (state, data) {
        for (const key in data) {
            state[key] = data[key]
            if (key === 'picData') {
                state[key] = setPicUrl(data[key])
            }
        }
    },
    setMoreData (state, data) {
        state.picData = [...state.picData, ...data.picData]
        state.picData = setPicUrl(state.picData)
        // console.log(state.picData, 'state.picData')
        state.total = data.total
    }
}
export const actions = {
    async getData ({ commit, dispatch }, params) {
        // const cateList = await wallpaper.getCategory()
        const state = {}
        let newestList = await wallpaper.queryByPage(params)
        // const newestList = await wallpaper.unsplashList(params)
        // console.log(list.datas, newestList.data, 'list')
        newestList.data = newestList.datas
        newestList.data.list = newestList.datas.list.map(el => ({ ...el, url: el.img }))
        newestList.data.total_count = newestList.datas.total
        const customCate = [{
            category: 'unsplash',
            hot_tag: [],
            position: '',
            show_name: 'unsplash'
        }]
        state.cateList = [{
            category: '最新',
            hot_tag: [],
            old_id: 0,
            position: '',
            show_name: '最新'
        },
        // ...cateList.data || [],
        ...customCate]
        state.picData = newestList.data.list
        state.total = newestList.data.total_count
        commit('setData', state)
        return Promise.resolve({ code: 200, success: true })
    },
    async getListByCategory ({ commit, dispatch }, params) {
        let newestList = []
        const state = {}
        newestList = await wallpaper.unsplashList({ cids: params.old_id, count: params.count })
        state.picData = newestList.data.list
        state.total = newestList.data.total_count
        commit('setData', state)
        return Promise.resolve({ code: 200, success: true })
    },
    async loadingMore ({ commit, dispatch }, params) {
        let newestList = []
        const state = {}
        if (params.old_id) {
            newestList = await wallpaper.GetListByCategory({
                pageno: params.pageno,
                cids: params.old_id,
                count: params.count
            })
        } else if (params.category === 'unsplash') {
            newestList = await wallpaper.unsplashList({
                pageno: params.pageno,
                count: params.count
            })
        } else {
            newestList = await wallpaper.queryByPage({
                pageno: params.pageno,
                count: params.count
            })
            newestList.data = newestList.datas
            newestList.data.list = newestList.datas.list.map(el => ({ ...el, url: el.img }))
            newestList.data.total_count = newestList.datas.total
            // newestList = await wallpaper.newestList({
            //     pageno: params.pageno,
            //     count: params.count
            // })
        }
        state.picData = [...newestList.data.list]
        state.total = newestList.data.total_count
        commit('setMoreData', state)
        return Promise.resolve({ code: 200, success: true })
    }
}
